//
//  ViewItemViewController.m
//  Feed Rider
//
//  Created by Adrian-Sergiu Nicuta on 17/10/11.
//  Copyright (c) 2011 Adrisen Technologies SPRL. All rights reserved.
//

#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MFMessageComposeViewController.h>
#import <Twitter/Twitter.h>
#import "ViewItemViewController.h"

@implementation ViewItemViewController

@synthesize CurrentItem;
@synthesize ItemWebView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = CurrentItem.title;
    
    UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionButtonClick)];
    self.navigationItem.rightBarButtonItem = actionButton;
    [actionButton release];

    [ItemWebView loadHTMLString:CurrentItem.summaryHTML baseURL:[NSURL URLWithString:@"/"]];
}

- (void)actionButtonClick
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Feed item" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Share by email", @"Share by message", @"Open link", @"Tweet", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
    [actionSheet showInView:self.view];
    [actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
        if (controller) {
            controller.mailComposeDelegate = self;
            [controller setSubject:CurrentItem.title];
            [controller setMessageBody:CurrentItem.summaryHTML isHTML:YES];
            [self presentModalViewController:controller animated:YES];
            [controller release];
        }
    } else if (buttonIndex == 1) {
        MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
        if (controller) {
            controller.messageComposeDelegate = self;
            [controller setBody:CurrentItem.summary];
            [self presentModalViewController:controller animated:YES];
            [controller release];
        }
    } else if (buttonIndex == 2) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:CurrentItem.link]];
    } else if (buttonIndex == 3) {
        TWTweetComposeViewController *controller = [[TWTweetComposeViewController alloc] init];
        [controller addURL:[NSURL URLWithString:CurrentItem.link]];
        [controller setInitialText:CurrentItem.title];
        [self presentModalViewController:controller animated:YES];
        controller.completionHandler = ^(TWTweetComposeViewControllerResult result)
        {
            [self dismissModalViewControllerAnimated:YES];
        };
        
        [controller release];
    }
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidUnload
{
    [self setItemWebView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [ItemWebView release];
    [super dealloc];
}
@end
